package cn.com.winning.ecare.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import cn.com.winning.ecare.common.Grideviewmenu;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.SlidingLayout;
import cn.com.winning.ecare.utils.URLUtils;
import cn.com.winning.ecare.utils.UserRecord;
import cn.com.winning.ecare.widgets.CustomProgressDialog;

/**临床路径-科室
 * @author Administrator
 *
 */
public class ClinicPathOne extends Activity
{
	private TextView tv1_current = null;
	private ListView lv = null;
	private ClinicPathOne oThis = this;
	private CustomProgressDialog proDialog;// 加载框
	private Map<String, Object> map_clinic; // 科室map
	List<Map<String, Object>> list_clinic = new ArrayList<Map<String, Object>>();// 科室list
	// private int[] arrPath = new int[] { R.drawable.ico_paths_01,
	// R.drawable.ico_paths_02, R.drawable.ico_paths_03,
	// R.drawable.ico_paths_04, R.drawable.ico_paths_05,
	// R.drawable.ico_paths_06, R.drawable.ico_paths_07,
	// R.drawable.ico_paths_08, R.drawable.ico_paths_09,
	// R.drawable.ico_paths_10, R.drawable.ico_paths_11,
	// R.drawable.ico_paths_12, R.drawable.ico_paths_13,
	// R.drawable.ico_paths_14, R.drawable.ico_paths_15,
	// R.drawable.ico_paths_16, R.drawable.ico_paths_17,
	// R.drawable.ico_paths_18, R.drawable.ico_paths_19,
	// R.drawable.ico_paths_20, R.drawable.ico_paths_21,
	// R.drawable.ico_paths_22, R.drawable.ico_paths_23 };

	private int preID = -1;
	private MyAdapter myAdapter;

	/**
	 * 侧滑布局对象，用于通过手指滑动将左侧的菜单布局进行显示或隐藏。
	 */
	private SlidingLayout slidingLayout;

	private LinearLayout content;
	Grideviewmenu gm;
	private GridView gridView;
	/**
	 * menu按钮，点击按钮展示左侧布局，再点击一次隐藏左侧布局。
	 */
	private ImageButton menuButton;
	private TextView nametv, tv;
	private ImageButton mainselected;
	MyApplication myApp;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{

		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.clinicpath_one);
		myApp = (MyApplication) getApplication();
		myApp.addActivity(this);
		myApp.getUserRecords().setUsername(myApp.getAccount());
		myApp.getUserRecords().setModule("ClinicPathOne@临床路径");
		UserRecord.getInstance(myApp.getUserRecords()).init();
		// 设置滑动菜单
		initSliding();
		// 初始化view
		findView();

		// 初始化界面数据
		GetData();

		// 点击事件
		setOnClick();
		
		
	}

	private void initSliding()
	{
		
		menuButton = (ImageButton) findViewById(R.id.menuButton);
		menuButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				
				oThis.onBackPressed();
			}
		});
		

	}

	private void setOnClick()
	{
		lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
			{

				myAdapter.notifyDataSetChanged();

				TextView tv1_mc = (TextView) arg1.findViewById(R.id.tv1_mc);
				TextView tv1_id = (TextView) arg1.findViewById(R.id.tv1_id);
				String str2_mc = tv1_mc.getText().toString();
				String str2_id = tv1_id.getText().toString();
				Intent intent = new Intent(ClinicPathOne.this, ClinicPathTwo.class);
				intent.putExtra("str2_mc", str2_mc);
				intent.putExtra("str2_id", str2_id);
				startActivity(intent);
				preID = arg2;

			}
		});
		mainselected.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View view)
			{

				Intent intent = new Intent();
				intent.setClass(oThis, Mainmean.class);
				startActivity(intent);
				oThis.finish();
			}
		});
	}

	private void findView()
	{
		lv = (ListView) this.findViewById(R.id.clinic_lv1);
		tv1_current = (TextView) this.findViewById(R.id.toptitle);
		mainselected = (ImageButton) this.findViewById(R.id.mainselected);

	}

	private void GetData()
	{
		proDialog = CustomProgressDialog.createDialog(oThis);
		proDialog.setMessage("数据加载中...");
		proDialog.show();
		proDialog.setCanceledOnTouchOutside(false);
		// 这里单开一个线程和后台数据交互
		new Thread(new LoadData()).start();

	}

	class LoadData implements Runnable
	{

		public void run()
		{

			Looper.prepare();
			iniData();
			Looper.loop();
		}
	}

	private void iniData()
	{
		try
		{
			Message msg = myHandler.obtainMessage();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			// ls.add(new BasicNameValuePair("userid", myApp.getAccount()));
			ls.add(new BasicNameValuePair("id", "0"));
			JSONObject json = HTTPGetTool.getTool().post(URLUtils.HOST + URLUtils.CLINICPATH, ls);
			List<Map<String, Object>> tempmain = new ArrayList<Map<String, Object>>();
			Map<String, Object> tempmap = new HashMap<String, Object>();
			if (json != null)
			{
				// 获取科室信息
				// JSONArray jsonArr = new JSONArray(json.getString("tl"));
				JSONArray jsonArr = json.getJSONArray("orglist");
				JSONObject js = null;
				for (int i = 0; i < jsonArr.length(); i++)
				{
					tempmap = new HashMap<String, Object>();
					js = jsonArr.getJSONObject(i);
					tempmap.put("id", js.getString("id"));
					tempmap.put("ljmc", js.getString("ljmc"));
					tempmain.add(tempmap);
				}
				msg.what = 0;
				msg.obj = tempmain;
			}
			else
			{
				msg.what = -1;
				msg.obj = "网络连接错误！";
			}

			myHandler.sendMessage(msg);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 主线程状态更新
	 */
	Handler myHandler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case -1:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					Toast.makeText(oThis, (String)msg.obj, Toast.LENGTH_SHORT).show();
					break;
				case 0:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					list_clinic = (List<Map<String, Object>>) msg.obj;
					if (myAdapter == null)
					{
						myAdapter = new MyAdapter();
					}
					lv.setAdapter(myAdapter);
					myAdapter.notifyDataSetChanged();
					break;
				default:
					break;
			}
		};
	};

	class MyAdapter extends BaseAdapter
	{

		@Override
		public int getCount()
		{

			return list_clinic.size();
		}

		@Override
		public Object getItem(int position)
		{

			return list_clinic.get(position);
		}

		@Override
		public long getItemId(int position)
		{

			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{

			View view = convertView;
			Map<String, Object> map = (Map<String, Object>) getItem(position);
			view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.clinicpath_item_one, null);
			TextView tv_id = (TextView) view.findViewById(R.id.tv1_id);
			tv_id.setText(map.get("id").toString());
			TextView tv_mc = (TextView) view.findViewById(R.id.tv1_mc);
			tv_mc.setText(map.get("ljmc").toString());
			if (position % 2 == 0)
			{// 偶数
				if (position == preID)
					view.setBackgroundColor(getResources().getColor(R.color.listhead_color));
				else
					view.setBackgroundColor(getResources().getColor(R.color.backgroundMYD));
			}
			else
			{// 奇数
				if (position == preID)// 选中
					view.setBackgroundColor(getResources().getColor(R.color.listhead_color));
				else
					view.setBackgroundColor(getResources().getColor(R.color.white_bg));
			}

			return view;
		}

	}

}
